package com.kchouse.mine;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kchouse.MainActivity;
import com.kchouse.R;
import com.kchouse.StartActivity;
import com.kchouse.bean.HostItem;
import com.kchouse.common.API;
import com.kchouse.common.BaseActivity;
import com.kchouse.common.MyHttpCycleContext;
import com.kchouse.common.ToastUtils;
import com.kchouse.homepage.GoodsDetailsActivity;
import com.kchouse.homepage.PrestoreDetailsActivity;
import com.squareup.picasso.Picasso;

import java.text.DecimalFormat;
import java.util.ArrayList;

import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

public class CollectionGoodsActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private TextView tv_title;
    private CollectionGoodsActivity mContent;
    private ImageView iv_back;
    private ListView lv_item;
    private ArrayList<HostItem> listCollect = new ArrayList<>();
    private CGAdapter adapter;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collection_goods);

        mContent = this;
        intiData();
        intiView();
    }

    private void intiView() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("收藏商品");
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);

        lv_item = (ListView) findViewById(R.id.lv_item);
        adapter = new CGAdapter();
        lv_item.setAdapter(adapter);
        lv_item.setOnItemClickListener(this);
    }

    private void intiData() {
        intent = getIntent();
//        GetCollectionList();
    }

    @Override
    protected void onResume() {
        super.onResume();
        GetCollectionList();
    }

    /**
     * 获取收藏套餐
     */
    private void GetCollectionList() {
        String url = API.GetCollectionList + MainActivity.userid;
        RequestParams params = new RequestParams(new MyHttpCycleContext());//请求参数
        params.addHeader("token", MainActivity.token);//添加header信息
        params.addFormDataPart("userId", MainActivity.userid);

        HttpRequest.get(url, params, new BaseHttpRequestCallback<String>() {

            //请求网络前
            @Override
            public void onStart() {
                showdialog();
            }

            @Override
            protected void onSuccess(String str) {
                Log.e("==", str);
                JSONObject json = JSONObject.parseObject(str);
                if (json.getBoolean("successful")) {
                    String datastr = json.getString("data");
                    JSONObject data = JSONObject.parseObject(datastr);
                    listCollect.clear();
                    listCollect.addAll(JSONObject.parseArray(data.getString("userCollectList"), HostItem.class));
                    adapter.notifyDataSetChanged();
                } else {
                    String datastr = json.getString("data");
                    JSONObject data = JSONObject.parseObject(datastr);
                    ToastUtils.getInstanc(mContent).showToast(data.getString("msg"));
                }
            }

            //请求失败（服务返回非法JSON、服务器异常、网络异常）
            @Override
            public void onFailure(int errorCode, String msg) {
//                toast("网络异常~，请检查你的网络是否连接后再试");
                Log.e("==", msg);
            }

            //请求网络结束

            @Override
            public void onFinish() {
                dismissdialog();
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        HostItem item = listCollect.get(position);
        if (item.getPublishType().equals("PRESTORE"))//是否是预存商品
        {
            intent.putExtra("prestoreId", item.getId());
            intent.setClass(mContent, PrestoreDetailsActivity.class);
        } else {
            intent.putExtra("isDir", false);
            intent.putExtra("id", item.getId());
            intent.setClass(mContent, GoodsDetailsActivity.class);
        }

        startActivity(intent);
    }

    class CGAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return listCollect.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
//            View view=getLayoutInflater().inflate(R.layout.lv_home_item,null);
//
//            TextView tv_original = (TextView) view.findViewById(R.id.tv_original);
//            tv_original.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//添加下划线


            HostItem item = listCollect.get(position);
            View view = getLayoutInflater().inflate(R.layout.lv_home_item, null);
            TextView tv_original = (TextView) view.findViewById(R.id.tv_original);
//                tv_original.setText(MyUtils.getdouble(item.getOriginaolPrice()));
            tv_original.setText(item.getOriginalPrice() + "");
            tv_original.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//添加下划线
            ImageView iv_item = (ImageView) view.findViewById(R.id.iv_item);
            Picasso.with(mContent).load(item.getUrl()).error(R.mipmap.ic_loadingerror).into(iv_item);
            TextView tv_price = (TextView) view.findViewById(R.id.tv_price);
//                tv_price.setText(MyUtils.getdouble(item.getPrice()));
            tv_price.setText(item.getPrice() + "");
            TextView tv_sales = (TextView) view.findViewById(R.id.tv_sales);
            tv_sales.setText("销量：" + (item.getSales() + item.getRealSales()));
            TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
            tv_title.setText(item.getName());
            TextView tv_giving = (TextView) view.findViewById(R.id.tv_giving);

            if (item.getCouponLimit() == 0) {
                tv_giving.setText("");
            } else {
                tv_giving.setText("最高抵扣" + (item.getCouponLimit() * 10) + "元");
            }
            TextView tv_merchantName = (TextView) view.findViewById(R.id.tv_merchantName);
            tv_merchantName.setText(item.getMerchantShortName());
            if (item.isFollowStatus()) {
                tv_merchantName.setTextColor(getResources().getColor(R.color.text_main));
                tv_merchantName.setTypeface(Typeface.DEFAULT_BOLD);

            } else {
                tv_merchantName.setTypeface(Typeface.DEFAULT);
                tv_merchantName.setTextColor(getResources().getColor(R.color.colorblack));
            }
            TextView tv_distance = (TextView) view.findViewById(R.id.tv_distance);
            tv_distance.setVisibility(View.INVISIBLE);
            return view;
        }
    }

}
